using System;
using System.Collections;
using System.Collections.Generic;
using Unity.VisualScripting;
using UnityEngine;

public class TimeManager : Singleton<TimeManager>
{
    [HideInInspector]public int gameSecond, gameMinute, gameHour, gameDay, gameMouth, gameYear;
    
    //游戏暂停时时间暂停
    public bool gameClockPause;

    private float tikTime;

    public TimeSpan GameTime => new TimeSpan(gameHour, gameMinute, gameSecond);
    private float timeDifference;

    public int diveSecond, diveMinute, diveHour;

    private void OnEnable()
    {
        EventHandler.EndDive += OnEndDive;
        EventHandler.StartDive += OnStartDive;
    }

    private void OnDisable()
    {
        EventHandler.EndDive -= OnEndDive;
        EventHandler.StartDive -= OnStartDive;
    }

    private void Update()
    {
        if (!gameClockPause)
        {
            tikTime += Time.deltaTime;
            if (tikTime >= Settings.secondThrshold)
            {
                tikTime -= Settings.secondThrshold;
                UpdateGameTime();
                // Debug.Log(GameTime);
            }
        }

        // if (Input.GetKey(KeyCode.T))
        // {
        //     ChectCode();
        // }

        
    }

    private void ChectCode()
    {
        for (int i = 0; i < 10; i++)
        {
            UpdateGameTime();
        }
    }

    private void NewGameTime()
    {
        gameSecond = 0;
        gameMinute = 0;
        gameHour = 7;
        gameDay = 1;
        gameMouth = 1;
    }

    public void UpdateGameTime()
    {
        gameSecond++;
        diveSecond++;
        if (gameSecond > Settings.secondHold)
        {
            gameMinute++;
            diveMinute++;
            gameSecond = 0;
            diveSecond = 0;
            if (gameMinute > Settings.minuteHold)
            {
                gameHour++;
                diveHour++;
                gameMinute = 0;
                diveMinute = 0;
                if (gameHour > Settings.hourHold)
                {
                    gameDay++;
                    gameHour = 0;
                    if (gameDay > Settings.dayHold)
                    {
                        gameMouth++;
                        gameDay = 1;
                    }
                }
            }
            EventHandler.CallUpdateAir();
        }
    }
    
    private void OnStartDive()
    {
        gameClockPause = false;
        diveHour = 0;
        diveMinute = 0;
    }
    
    private void OnEndDive()
    {
        gameClockPause = true;
        gameHour += 4;
    }
}
